    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.*;

import javax.swing.*;

import java.util.LinkedList;
import java.util.HashSet;

public class PowerList extends JLayeredPane {
    private static PowerList instance = null;
    
    private LinkedList<PowerListItem> items;
    private String pool_id;
    private PowerTypeTag framework;

    private PowerList() {}
    
    public void init() {
        
        setLayout(new GridBagLayout());
        
        items = new LinkedList<PowerListItem>();
    }
    
    public void redraw() {
        removeAll();
        
        GridBagConstraints c = new GridBagConstraints();
        
        int i = 0, j = 0;
        for(PowerListItem p : items) {
            if(p.isVisible()) {
                if(p instanceof Power) {
                    i += j % 2;
                    j++;
                    c.gridx = j % 2;
                    c.gridwidth = 1;
                    c.fill = GridBagConstraints.NONE;
                } else {
                    i++;
                    j = 1;
                    c.gridx = 0;
                    c.gridwidth = 2;
                }
                c.anchor = GridBagConstraints.FIRST_LINE_START;
                c.gridy = i;
                add(p, c);
            }
        }
        PowerTypeTag filler = new PowerTypeTag("");
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        c.weightx = 0.5;
        c.weighty = 100.0;
        c.gridwidth = 2;
        c.gridx = 0;
        c.gridy = i;
        add(filler, c);
    }
    
    /**
     * updates the visible powers according to the power in the HashSet
     */
    public void update(HashSet<String> set) {
        PowerListItem last_tag = null;
        for(PowerListItem item : items) {
            if (item instanceof PowerTypeTag) {
                last_tag = item;
                item.setVisible(false);
            } else {
                if(set.contains(item.getId())) {
                    item.setVisible(true);
                    if(last_tag != null)
                        last_tag.setVisible(true);
                } else
                    item.setVisible(false);
            }
        }
        redraw();
    }
    
    public void addItem(PowerListItem item) {
        items.add(item);
    }
    
    public void addItem(Power item) {
        items.add(item);
    }
    
    public void addItem(Advantage item) {
        items.add(item);
    }
    
    public void addItem(PowerTypeTag item) {
        items.add(item);
    }
    
    public void add(Framework item) {
        items.add(item);
    }
    
    public String getId() {
        return pool_id;
    }
    
    public void setFrameworkVisible(boolean visible) {
        framework.setVisible(visible);
    }
    
    /**
     * update the PowerTypeTags to the current language
     */
    public void updateLanguage() {
        PowerTypeTag tag;
        for(PowerListItem item : items) {
            if(item instanceof PowerTypeTag) {
                tag = (PowerTypeTag) item;
                tag.redraw();
            }
        }
    }
    
    public static PowerList getInstance() {
        if(instance == null)
            instance = new PowerList();
        return instance;
    }
}
